﻿using Microsoft.AspNetCore.Mvc;
using SmartParking.Dto.Req;
using SmartParking.Dto.Resp;
using SmartParking.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace SmartParking.Controller
{
    public class PricingPlanController : ApplicationService
    {
        private readonly IRepository<PricingPlan> _pricingPlanRepository;
        private readonly IRepository<MultistepPrice> _multistepDb;
        private readonly IRepository<ParkingRecord> _parkingRecordDb;
        public PricingPlanController(IRepository<PricingPlan> pricingPlanRepository, IRepository<MultistepPrice> multistepDb, IRepository<ParkingRecord> parkingRecordDb)
        {
            _pricingPlanRepository = pricingPlanRepository;
            _multistepDb = multistepDb;
            _parkingRecordDb = parkingRecordDb;
        }

        [HttpPost]
        public virtual async Task<JsonResult> Insert([FromBody] PricingPlanReq req)
        {
            var newPlan = ObjectMapper.Map<PricingPlanReq, PricingPlan>(req);
            newPlan = await _pricingPlanRepository.InsertAsync(newPlan);
            var newStepPrice = new MultistepPrice
            {
                PlanId = newPlan.Id,
                LowerLimit = 0,
                UpperLimit = double.MaxValue,
                Price = 0
            };
            newStepPrice = await _multistepDb.InsertAsync(newStepPrice);
            return new JsonResult(newPlan);
        }
        [HttpPost]
        public virtual async Task<IActionResult> CheckEditable([FromBody] PricingPlanReq req)
        {
            var query = await _pricingPlanRepository.GetQueryableAsync();
            query.Where(p => p.StartDate <= req.StartDate && p.EndDate > req.StartDate)
               .Where(p => p.StartDate <= req.EndDate && p.EndDate > req.EndDate)
               .Where(p => p.Id != req.Id);
            if (query.Any())
            {
                throw new UserFriendlyException("时间范围冲突");
            }
            else
            {
                return new OkResult();
            }
        }
        [HttpPost]
        public virtual async Task<IActionResult> List([FromBody] PricingPlanPageReq req)
        {
            var query = await _pricingPlanRepository.WithDetailsAsync(p => p.Prices);
            query = query.WhereIf(!string.IsNullOrWhiteSpace(req.Name), p => p.Name.Contains(req.Name))
                .OrderBy(p => p.StartDate);
            var total = query.Count();
            var list = query.Skip(req.SkipCount).Take(req.MaxResultCount).ToList();
            var result = new PricingPlanPagedResp
            {
                TotalCount = total,
                Items = ObjectMapper.Map<List<PricingPlan>, List<PricingPlanResp>>(list)
            };
            return new JsonResult(result);
        }
        [HttpPut]
        public virtual async Task<IActionResult> Update([FromBody] PricingPlanReq req)
        {
            var updatePlan = ObjectMapper.Map<PricingPlanReq, PricingPlan>(req);
            updatePlan = await _pricingPlanRepository.UpdateAsync(updatePlan);
            return new JsonResult(updatePlan);
        }
        [HttpGet]
        public virtual async Task<IActionResult> CheckDeletable([FromQuery] Guid id)
        {
            var query = await _parkingRecordDb.GetQueryableAsync();
            query = query.Where(r => r.PlanId == id);
            if (query.Any())
            {
                throw new UserFriendlyException("已有车辆采用该方案");
            }
            else
            {

                return new OkResult();
            }
        }
        [HttpDelete]
        public virtual async Task<IActionResult> Delete([FromQuery] Guid id)
        {
            await _multistepDb.DeleteAsync(s => s.PlanId == id);
            await _pricingPlanRepository.DeleteAsync(p => p.Id == id);
            return new OkResult();
        }
    }
}
